package com.webserver.servlet;

import com.webserver.http.HttpContext;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 *  用于处理请求的Servlet
 *  ClientHandler处理请求的环节调用当前类的service方法完成处理细节
 */
public class DispatcherServlet {
    public void service(HttpRequest request, HttpResponse response) {
        //通过request获取抽象路径中的请求部分
        String path = request.getRequestURI();//获取抽象路径
        System.out.println("请求路径========> " + path);

        //首先判断是否为请求一个业务
        if ("/myweb/regUser".equals(path)) {
            //如果是请求注册业务,就处理注册
            RegServlet servlet = new RegServlet();
            servlet.service(request, response);
        } else if("/myweb/loginUser".equals(path)) {
            LoginServlet servlet = new LoginServlet();
            servlet.service(request,response);
        }else if ("/myweb/createArticleUser".equals(path)) {
            CreateArticleServlet servlet = new CreateArticleServlet();
            servlet.service(request,response);
        }else {
            File file = new File("./webapps" + path);
            if (file.exists() && file.isFile()) {//是否存在
                response.setEntity(file);
            } else {
                File notFoundPage = new File("./webapps/root/404.html");
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                response.setEntity(notFoundPage);
            }
        }

        //设置通用响应头
        response.addHeader("Server", "WebServer");

    }
}
